package stackdemo;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 涛
 * Date: 2023-10-05
 * Time: 10:35
 */
//牛课题. 出栈入栈次序匹配
public class Soloution2 {
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        // write code here
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        //遍历一遍pushv数组一遍,把元素放到栈上
        for(int i = 0; i < pushV.length; i++) {
            stack.push(pushV[i]);
            //判断栈顶元素和popv[j]的元素是否相等，相等，要出栈，不相等就继续遍历pushV数组
            while(j < popV.length && !stack.empty() && stack.peek() == popV[j]) {
                stack.pop();
                j++;
            }
        }
        return stack.empty();
    }
}
